package cn.kgc.spring04.convertor;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: mosin
 * @version: v1.0  2022/12/1
 * 自定义converter
 * 编写类实现Converter<k,v>接口     k：原始数据类型  v:目标数据类型    <String,Date>   String  -> Date
 */
public class MyDateConverter implements Converter<String, Date> {

    private String pattern;

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    // 2022/12/1   2022-12-1
    @Override
    public Date convert(String source) {
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        try {
            Date date = simpleDateFormat.parse(source);
            return  date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


}
